The FORMAT keyword can be used with the formatted input/output routines to explicitly specify the appearance of the data. The standard syntax of IDL format strings is similar to that used in FORTRAN; a C printf()-style syntax is also supported, as described in C printf-Style Quoted String Format Code.
Note: IDL uses the standard I/O function sprintf to do its formatting. Different platforms implement this function in different ways, which may lead to slight inconsistencies in the appearance of the output.
The format string specifies the format in which data is to be transferred as well as the data conversion required to achieve that format. The format specification strings supplied by the FORMAT keyword have the form:
FORMAT = '(q1f1s1f2s2 ... fnqn)'
where q, f, and s are described below.
q is zero or more slash (/) record terminators. On output, each record terminator causes the output to move to a new line. On input, each record terminator causes the next line of input to be read.
f is a format code. Some format codes specify how data should be transferred while others control some other function related to how input/output is handled.The code f can also be a nested format specification enclosed in parentheses. This is called a group specification and has the following form:
...[n](q1f1s1f2s2 ... fnqn) ...
A group specification consists of an optional repeat count n followed by a format specification enclosed in parentheses. Use of group specifications allows more compact format specifications to be written. For example, the format specification:
FORMAT = '("Result: ", "<",I5,">", "<",I5,">")'
can be written more concisely using a group specification:
FORMAT = '("Result: ", 2("<",I5,">"))'
If the repeat count is 1 or is not given, the parentheses serve only to group format codes for use in format reversion (discussed in the next section). Format codes and their syntax are described in detail in Format Codes.
s is a field separator. A field separator consists of one or more commas (,) and/or slash record terminators (/). The only restriction is that two commas cannot occur side-by-side.
The arguments provided in a call to a formatted input/output routine are called the argument list. The argument list specifies the data to be moved between memory and the file. All data are handled in terms of basic IDL components. Thus, an array is considered to be a collection of scalar data elements, and a structure is processed in terms of its basic components. Complex scalar values are treated as two floating-point values.
IDL uses the following rules to process explicitly formatted input/output:
When a format code that does not transfer data to or from the argument list is encountered, process it according to its meaning. The format codes that do not transfer data to or from the argument list are summarized here.
Code |
Action |
Quoted String |
On output, the contents of the string are written out. On input, quoted strings are ignored. |
: |
The colon format code in a format string terminates format processing if no more items remain in the argument list. It has no effect if data still remains on the list. |
$ |
On output, if a $ format code is placed anywhere in the format string, the new line implied by the closing parenthesis of the format string is suppressed. On input, the $ format code is ignored. |
nH |
FORTRAN-style Hollerith string. Hollerith strings are treated exactly like quoted strings. |
nX |
Skips n character positions. |
Tn |
Tab. Sets the character position of the next item to the n-th position in the current record. |
TLn |
Tab Left. Specifies that the next character to be transferred to or from the current record is the n-th character to the left of the current position. |
TRn |
Tab Right. Specifies that the next character to be transferred to or from the current record is the n-th character to the right of the current position. |
When a format code that transfers data to or from the argument list is encountered, it is matched up with the next datum in the argument list. The format codes that transfer data to or from the argument list are summarized in the following table.
Code |
Action |
A |
Transfer character data. |
B |
Transfer binary data. |
C() |
Transfer calendar (Julian date and/or time) data. |
D |
Transfer double-precision, floating-point data. |
E |
Transfer floating-point data using scientific (exponential) notation. |
F |
Transfer floating-point data. |
G |
Use F or E format depending on the magnitude of the value being processed. |
I |
Transfer integer data. |
O |
Transfer octal data. |
Q |
Obtain the number of characters in the input record remaining to be transferred during a read operation. In an output statement, the Q format code has no effect except that the corresponding input/output list element is skipped. |
Z |
Transfer Hexadecimal data. |
In format reversion, the current record is terminated, a new one is initiated, and format control reverts to the group repeat specification whose opening parenthesis matches the next-to-last closing parenthesis of the format string. If the format does not contain a group repeat specification, format control returns to the initial opening parenthesis of the format string. For example, the IDL command:
PRINT, FORMAT = '("The values are: ", 2("<", I1, ">"))', $
INDGEN(6)
results in the output
The values are: <0><1>
<2><3>
<4><5>
The process involved in generating this output is as follows: